﻿using System;
using Orchard.DisplayManagement;
using Orchard.Forms.Services;
using Orchard.Localization;

namespace Amba.Workflow.Activities
{
    public class WriteWorkflowLogActivityForms : IFormProvider
    {
        protected dynamic Shape { get; set; }
        public Localizer T { get; set; }

        public WriteWorkflowLogActivityForms(IShapeFactory shapeFactory)
        {
            Shape = shapeFactory;
            T = NullLocalizer.Instance;
        }

        public void Describe(DescribeContext context)
        {
            Func<IShapeFactory, dynamic> form =
                shape => Shape.Form(
                    Id: "WriteWorkflowLogActivityForms",
                    _LogMessage: Shape.Textarea(
                        Id: "LogMessage", Name: "LogMessage",
                        Title: T("Log Message"),
                        Description: T("Message for event log"),
                        Classes: new[] { "tokenized" }
                        )
                    );
            context.Form("WriteWorkflowLogActivityForms", form);
        }
    }

    public class WriteWorkflowLogActivityFormsValidator : IFormEventHandler
    {
        public Localizer T { get; set; }

        public void Building(BuildingContext context)
        {
        }

        public void Built(BuildingContext context)
        {
        }

        public void Validating(ValidatingContext context)
        {
            if (context.FormName == "WriteWorkflowLogActivityForms")
            {
                if (context.ValueProvider.GetValue("Body").AttemptedValue == String.Empty)
                {
                    context.ModelState.AddModelError("Body", T("You must provide a Body").Text);
                }
            }
        }

        public void Validated(ValidatingContext context)
        {
        }
    }
}